// pages/user/editCustomer/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    userTel: '',
    userAddress: '',
    longitude: '',
    latitude: '',
    CUST_NAME: '',
    id: ''
  },

  onLoad(options) {
    this.getData(options)
  },

  getData(e) {
    const self = this;
    const url = "https://erp.yilestar.com/weapp/customer/getCustDetail";
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      id: e.id
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url,
      data,
      method: 'post',
      success: (res) => {
        const datas = res.data.data;
        wx.hideLoading()
        self.setData({
          userName: datas.CUST_NAME,
          userTel: datas.TEL,
          userAddress: datas.ADDR,
          CUST_NAME: datas.CUST_NAME,
          id: datas.id
        })
      }
    })
  },
  // 修改客户
  editCustomer() {
    const self = this;
    const url = "https://erp.yilestar.com/weapp/customer/edit";
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      CUST_NAME: self.data.CUST_NAME,
      ADDR: self.data.userAddress,
      TEL: self.data.userTel,
      CONT_PRSON: self.data.userName,
      INI_FLG: 0, // 用户禁用标识
      longitude: self.data.longitude,
      latitude: self.data.latitude,
      id:self.data.id
    }
    clearTimeout(self.time)
    self.time = setTimeout(() => {
      wx.request({
        url,
        data,
        method: "post",
        success: (res) => {
          console.log(res)
          if (res.data.status == -1) {
            wx.showToast({
              title: '修改失败',
              icon: 'none',
              duration: 2000,
            })
            return;
          }
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000,
          })
          wx.navigateBack({
            dealt: 1
          })
        }
      })
    }, 1000)
  },

  userName(ev) {
    const self = this;
    clearTimeout(self.time);
    self.time = setTimeout(() => {
      self.setData({
        userName: ev.detail
      })
    }, 1000)
  },

  userTel(ev) {
    const self = this;
    clearTimeout(self.time);
    self.time = setTimeout(() => {
      self.setData({
        userTel: ev.detail
      })
    }, 1000)
  },

  // 选择地址
  handleCheck(e) {
    wx.chooseLocation({
      complete: (cb) => {
        console.log(cb)
        this.setData({
          latitude: cb.latitude,
          longitude: cb.longitude,
          userAddress: cb.address
        })
      },
    })
  },

  // 删除客户
  delCustomer() {
    const self = this;
    const url = "https://erp.yilestar.com/weapp/customer/del";
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      id: self.data.id
    }
    wx.showModal({
      title: '提示',
      content: '确认删除该用户信息？',
      showCancel: true,
      cancelText: '点错了',
      confirmText: '确定',
      success: (res) => {
        wx.request({
          url,
          data,
          method: "post",
          success: (res) => {
            console.log(res)
            if (res.data.status == -1) {
              wx.showToast({
                title: '删除失败',
                icon: 'none',
                duration: 2000,
              })
              return;
            }
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 2000,
            })
            wx.navigateBack({
              dealt: 1
            })
          }
        })
      },
    })
  }
})